package edu.whut.mall.admin.service.impl;

import com.github.pagehelper.PageHelper;
import edu.whut.mall.admin.domain.GroupBuyOrderSearchParams;
import edu.whut.mall.admin.enums.OrderStatusEnum;
import edu.whut.mall.admin.enums.UserRoleEnum;
import edu.whut.mall.admin.mapper.GroupBuyOrderCustomMapper;
import edu.whut.mall.admin.service.IGroupBuyOrderService;
import edu.whut.mall.admin.shiro.util.ShiroUtil;
import edu.whut.mall.admin.vo.GroupBuyOrderDetailVo;
import edu.whut.mall.admin.vo.GroupBuyOrderVo;
import edu.whut.mall.common.api.CommonPage;
import edu.whut.mall.mapper.GroupBuyOrderMapper;
import edu.whut.mall.model.GroupBuyOrder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @ClassName GroupBuyOrderServiceImpl
 * @Description
 * @Author lvfanbing
 * @Date 2020-11-09 17:01
 * @Version 1.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class GroupBuyOrderServiceImpl implements IGroupBuyOrderService {

    private final GroupBuyOrderCustomMapper groupBuyOrderCustomMapper;

    private final GroupBuyOrderMapper groupBuyOrderMapper;

    @Override
    public CommonPage<GroupBuyOrderVo> listGroupBuyOrderByCondition(GroupBuyOrderSearchParams groupBuyOrderSearchParams, Integer pageNum, Integer size) {
        PageHelper.startPage(pageNum, size);
        if (ShiroUtil.getAdminType().equals(UserRoleEnum.MERCHANT_ADMIN.getCode())) {
            groupBuyOrderSearchParams.setMerchantId(ShiroUtil.getMerchantId());
        }
        List<GroupBuyOrderVo> groupBuyOrderVoList = groupBuyOrderCustomMapper.listGroupBuyOrderByCondition(groupBuyOrderSearchParams);
        return CommonPage.restPage(groupBuyOrderVoList);
    }

    @Override
    public GroupBuyOrderDetailVo getGroupBuyOrderDetailByGroupBuyOrderId(Long groupBuyOrderId) {
        return groupBuyOrderCustomMapper.getGroupBuyOrderDetailByGroupBuyOrderId(groupBuyOrderId);
    }

    @Override
    public Integer groupBuyOrderConsign(Long orderId, Integer logisticsPlatform, String logisticsNumber) {
        GroupBuyOrder groupBuyOrder = new GroupBuyOrder();
        groupBuyOrder.setId(orderId);
        groupBuyOrder.setLogisticsPlatform(logisticsPlatform);
        groupBuyOrder.setLogisticsNumber(logisticsNumber);
        groupBuyOrder.setOrderStatus(OrderStatusEnum.SHIPPED.getCode());
        return groupBuyOrderMapper.updateByPrimaryKeySelective(groupBuyOrder);
    }

    @Override
    public Integer deleteOneGroupBuyOrder(Long orderId) {
        GroupBuyOrder groupBuyOrder = new GroupBuyOrder();
        groupBuyOrder.setIsDeleted(1);
        groupBuyOrder.setId(orderId);
        return groupBuyOrderMapper.updateByPrimaryKeySelective(groupBuyOrder);
    }
}
